<?php
class search extends Model{
	/**
	 * 搜索习题
	 * @param unknown_type $typeid
	 * @param unknown_type $stateid
	 * @param unknown_type $subjectid
	 * @param unknown_type $classid
	 */
	public function searchExercise($typeid,$stateid,$subjectid,$classid,$adminid,$keywords,$page=1,$pageSize=8)
	{
		$str ="SELECT `exercise`.*,`subject`.`sub_cont`,`admin`.`admin_realname`,`exercise_type`.`et_cont` FROM `exercise`
				LEFT JOIN `subject` ON `subject`.`sub_id`= `exercise`.`sub_id`
				LEFT JOIN `admin` ON `admin`.`admin_id`= `exercise`.`ex_creat_admin_id`
				LEFT JOIN `exercise_type` ON `exercise`.`et_id`= `exercise_type`.`et_id` ";
		$where =" WHERE ";
		$filter = " 1 ";
		if($typeid == 1){
			$filter.=" AND `exercise`.`ex_creat_admin_id` = '".$adminid."' ";
		}
		if ($stateid == 1) {
			$filter.=" AND `exercise`.`ex_status` = 1 ";
		}else if($stateid == 2){
			$filter.=" AND `exercise`.`ex_status` = 0 ";
		}
		if ($subjectid > 0)
		{
			$filter.=" AND `exercise`.`sub_id` = '".$subjectid."' ";
		}
		if ($classid > 0) {
			$filter.=" AND `exercise`.`et_id` = '".$classid."' ";
		}
		if($keywords !== ""){
			$filter .=" AND `ex_title` LIKE '%".$keywords."%' ";
		}
		$order = " ORDER BY `exercise`.`ex_creat_time` DESC ";
		$limit =" LIMIT ".($page-1)*$pageSize.",".$pageSize."";
		$sql = $str.$where.$filter.$order.$limit;
		//echo $sql;
		$list = $this->fetchAll($sql);
		$total = $this->getTotal('exercise',$filter);
		$totalPage = ceil($total / $pageSize);
		return array('page'=>$page,'list'=>$list,'total'=>$total,'totalPage'=>$totalPage);
		
	}
}